﻿#region License
// Copyright (c) 2012 Mund Consulting Ltd
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Administration;
using System.Web;
using System.Web.UI;
using Mund.Sharepoint.Publishing.WebParts;
using System.Resources;
using System.Web.Resources;

namespace Mund.Sharepoint.Publishing.WebParts.SearchBox
{
    class SearchBoxEditorPart : EditorPart
    {
        DropDownList ddlChooseItem;
        DropDownList ddlScopes;
        Label lblScopes;
        Label lblChooseItem;
        Label lblSetUrl;
        Label lblImageUrl;
        TextBox tbxSetUrl;
        TextBox tbxImageUrl;
        protected override void CreateChildControls()
        {

            tbxSetUrl = new TextBox();

            lblSetUrl = new Label();
            lblSetUrl.Text = (String)HttpContext.GetGlobalResourceObject("MundPublishingResource", "searchResultUrl");
            
           
            lblChooseItem = new Label();
            lblChooseItem.Text = (String)HttpContext.GetGlobalResourceObject("MundPublishingResource", "selectItem"); 

            lblScopes = new Label();
            lblScopes.Text = (String)HttpContext.GetGlobalResourceObject("MundPublishingResource", "selectScope"); 

            ddlScopes = new DropDownList();
            ddlScopes.Width = 130;
            ddlScopes.Height = 22;
            ddlScopes.Items.Add(new ListItem((String)HttpContext.GetGlobalResourceObject("MundPublishingResource", "curSiteCollection"), SPContext.Current.Site.Url));
            ddlScopes.Items.Add(new ListItem((String)HttpContext.GetGlobalResourceObject("MundPublishingResource", "curSites"), SPContext.Current.Web.Url));
            ddlScopes.Items.Add(new ListItem((String)HttpContext.GetGlobalResourceObject("MundPublishingResource", "listLibrary")));
            ddlScopes.AutoPostBack = true;
            ddlScopes.SelectedIndexChanged += new EventHandler(DdlScopes_SelectedIndexChanged);

            ddlChooseItem = new DropDownList();
            ddlChooseItem.Width = 130;
            ddlChooseItem.Height = 22;
            ddlChooseItem.Enabled = false;

            lblImageUrl = new Label();
            lblImageUrl.Text = (String)HttpContext.GetGlobalResourceObject("MundPublishingResource", "setImageUrl");

            tbxImageUrl = new TextBox();

            this.Controls.Add(lblSetUrl);
            this.Controls.Add(tbxSetUrl);
            this.Controls.Add(lblScopes);
            this.Controls.Add(ddlScopes);
            this.Controls.Add(lblChooseItem);
            this.Controls.Add(ddlChooseItem);
            this.Controls.Add(lblImageUrl);
            this.Controls.Add(tbxImageUrl);
        }

        void DdlScopes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlScopes.SelectedIndex == 2)
            {
                SPWeb currWeb = SPContext.Current.Web;
                lblChooseItem.Enabled = true;
                ddlChooseItem.Enabled = true;
                ddlChooseItem.Items.Clear();
                SPListCollection listCol = currWeb.Lists;
                foreach (SPList list in listCol)
                {
                    ddlChooseItem.Items.Add(new ListItem(list.Title, list.ParentWeb.Site.Url + list.ParentWebUrl + list.RootFolder.Url));
                }
            }
            else if (ddlScopes.SelectedIndex == 0)
            {
                lblChooseItem.Enabled = false;
                ddlChooseItem.Enabled = false;
            }
            else
            {
                lblChooseItem.Enabled = false;
                ddlChooseItem.Enabled = false;
            }
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            SearchBox searchBox = this.WebPartToEdit as SearchBox;
            searchBox.SetImageUrl = tbxImageUrl.Text;
            searchBox.SetUrl = tbxSetUrl.Text;
            if (ddlScopes.SelectedIndex == 2)
            {
                searchBox.ScopeOption = ddlChooseItem.SelectedValue;
            }
            else
            {
                searchBox.ScopeOption = ddlScopes.SelectedValue;
            }
            
            searchBox.Scope = ddlScopes.SelectedValue;

            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            SearchBox searchBox = this.WebPartToEdit as SearchBox;
            tbxImageUrl.Text = searchBox.SetImageUrl;
            tbxSetUrl.Text = searchBox.SetUrl;
            if (!string.IsNullOrEmpty(searchBox.ScopeOption))
            {
                ddlScopes.SelectedValue = searchBox.Scope;
                DdlScopes_SelectedIndexChanged(null, new EventArgs());
                ddlChooseItem.SelectedValue = searchBox.ScopeOption;

            }
            return;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.HeadDivStart());
            lblSetUrl.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.DivEnd());
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivStart());
            tbxSetUrl.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivEnd());

            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BottomLine());

            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.HeadDivStart());
            lblScopes.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.DivEnd());
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivStart());
            ddlScopes.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivEnd());

            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BottomLine());

            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.HeadDivStart());
            lblChooseItem.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.DivEnd());
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivStart());
            ddlChooseItem.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivEnd());

            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BottomLine());

            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.HeadDivStart());
            lblImageUrl.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.DivEnd());


            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivStart());
            tbxImageUrl.RenderControl(writer);
            writer.Write(Mund.Sharepoint.Publishing.WebParts.EditorRender.BodyDivEnd());

        }
    }
}
